/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.selfmainten;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.NetWorkCache;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.NwCatagoryDef;
import com.sinodata.bsm.common.vo.NwDeviceDef;
import com.sinodata.bsm.common.vo.NwDeviceIndicator;
import com.sinodata.bsm.common.vo.NwIndicatorDef;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-10-15 上午9:42:52          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class NetWorkAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private NetWorkCache netWorkCache;

    public String common() {
        return "common";
    }

    @Override
    public Object getModel() {
        return null;
    }

    public String view() {
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        request.setAttribute("name", netWorkCache.getDeviceMap().get(Long.parseLong(id)).getName());
        return "view";
    }

    @Override
    public String list() throws Exception {
        List<NwDeviceDef> devList = netWorkCache.getDevList();
        Map<Long, NwCatagoryDef> catMap = netWorkCache.getCatMap();
        Map<Long, List<String>> devOidMap = netWorkCache.getDevSysoidsMap();
        String alpha = request.getParameter("alpha");
        if (alpha == null || alpha.length() < 1) {
            alpha = "A";
        }

        JSONObject json = new JSONObject();
        JSONArray data = new JSONArray();
        try {

            for (NwDeviceDef dev : devList) {
                if (dev.getName().startsWith(alpha) || dev.getName().startsWith(alpha.toLowerCase())) {
                    String op = "<div style='width: 50px;align:center;'>&nbsp;&nbsp;<a href='javascript:view(" + dev.getId() + ");'><img src='../style/default/bsm/images/view.gif' title='查看详情'></a><div>";
                    JSONObject obj = new JSONObject();
                    obj.element("op", op);
                    obj.element("id", dev.getId());
                    obj.element("name", dev.getName());
                    List<String> oidsList = devOidMap.get(dev.getId());
                    String oids = "";
                    if (oidsList != null) {
                        for (int i = 0; i < oidsList.size(); i++) {
                            oids += oidsList.get(i);
                            if (i != oidsList.size() - 1) {
                                oids += "<br>";
                            }
                        }
                    }
                    obj.element("sysoid", "<div style='word-wrap:break-word; word-break:break-all;'>" + oids + "</div>");
                    obj.element("cat", catMap.get(dev.getCatagory()).getName());
                    obj.element("vendor", dev.getVendor());
                    data.add(obj);
                }
                if ("其它".equals(alpha)) {
                    int ascVal = dev.getName().toCharArray()[0];
                    if (ascVal >= 48 && ascVal <= 57) {
                        String op = "<div style='width: 50px;align:center;'>&nbsp;&nbsp;<a href='javascript:view(" + dev.getId() + ");'><img src='../style/default/bsm/images/view.gif' title='查看详情'></a><div>";
                        JSONObject obj = new JSONObject();
                        obj.element("op", op);
                        obj.element("id", dev.getId());
                        obj.element("name", dev.getName());
                        List<String> oidsList = devOidMap.get(dev.getId());
                        String oids = "";
                        if (oidsList != null) {
                            for (int i = 0; i < oidsList.size(); i++) {
                                oids += oidsList.get(i);
                                if (i != oidsList.size() - 1) {
                                    oids += "<br>";
                                }
                            }
                        }
                        obj.element("sysoid", "<div style='word-wrap:break-word; word-break:break-all;'>" + oids + "</div>");
                        obj.element("cat", catMap.get(dev.getCatagory()).getName());
                        obj.element("vendor", dev.getVendor());
                        data.add(obj);
                    }
                }
            }

            json.element("iTotalRecords", data.size());
            json.element("iTotalDisplayRecords", data.size());
            json.element("aaData", data);
            Struts2Utils.renderText(json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String indecList() {
        String id = request.getParameter("id");
        JSONObject json = new JSONObject();
        JSONArray data = new JSONArray();
        List<NwDeviceIndicator> devIndics = netWorkCache.getDevIndicMap().get(Long.parseLong(id));
        Map<Long, NwIndicatorDef> indicMap = netWorkCache.getIndicMap();

        if (devIndics != null) {
            for (NwDeviceIndicator devIndic : devIndics) {
                NwIndicatorDef indic = indicMap.get(devIndic.getIndic_id());
                JSONObject obj = new JSONObject();
                obj.element("id", indic.getId());
                obj.element("name", indic.getName());
                obj.element("oid", "<div class='oid_limit'><label title='" + indic.getOid() + "'>" + indic.getOid() + "</label></div>");
                obj.element("unit", indic.getYunit());
                obj.element("desc", "<div class='desc_limit'><label title='" + indic.getDescription() + "'>" + indic.getDescription() + "</label></div>");
                data.add(obj);
            }
        }

        json.element("iTotalRecords", data.size());
        json.element("iTotalDisplayRecords", data.size());
        json.element("aaData", data);
        Struts2Utils.renderText(json.toString());
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

}
